package com.itzmn.newcode.offer;

import java.util.ArrayList;

/**
 * @Author: 张梦楠
 * @Date: 2018/11/20 19:15
 * 简书：https://www.jianshu.com/u/d611be10d1a6
 * 码云：https://gitee.com/zhangqiye
 * @Description: 剑指Offer  查看手中的牌是否是顺子
 *
 *   思路：
 *    将数组中的0 取出，将剩下的排序，装入list
 *    从list中取出第一个，判断加加是否是第二个，不是就用0填，如果填不了就是false
 *
 */
public class IsStraight {

    public static void main(String[] args) {

        new IsStraight().init();
    }

    public void init(){


        isContinuous(new int[]{2,3,5,0,0});
    }

    public boolean isContinuous(int [] numbers) {

        if (numbers.length==0){
            return false;
        }

        for (int i=0;i<numbers.length-1;i++){
            for (int j=0;j<numbers.length-1-i;j++){

                //大的向后放
                if (numbers[j]>numbers[j+1]){

                    int temp = numbers[j];
                    numbers[j] = numbers[j+1];
                    numbers[j+1] = temp;
                }
            }
        }

        ArrayList<Integer> zero = new ArrayList<>();
        ArrayList<Integer> integers = new ArrayList<>();

        for (int i=0;i<numbers.length;i++){

            if (numbers[i] == 0){
                zero.add(numbers[i]);
            }else {
                integers.add(numbers[i]);
            }
        }

        ArrayList<Integer> list = new ArrayList<>();

        list.add(integers.remove(0));
        while (integers.size() != 0){

            if (list.get(list.size()-1)+1 == integers.get(0)){
                list.add(integers.remove(0));
            }else {
                if (zero.size()>0){
                    zero.remove(0);
                    list.add(list.get(list.size()-1)+1);
                }else {
                    return false;
                }
            }
        }
        return true;

    }

}
